import { Graph } from '@antv/x6';

export function registerShortcuts(graph) {
  // 删除快捷键
  graph.bindKey('delete', () => {
    const cells = graph.getSelectedCells();
    if (cells.length) {
      graph.removeCells(cells);
    }
  });

  // 复制快捷键
  graph.bindKey('ctrl+c', () => {
    const cells = graph.getSelectedCells();
    if (cells.length) {
      graph.copy(cells);
    }
  });

  // 粘贴快捷键
  graph.bindKey('ctrl+v', () => {
    if (!graph.isClipboardEmpty()) {
      const cells = graph.paste({ offset: 32 });
      graph.cleanSelection();
      graph.select(cells);
    }
  });

  // 撤销快捷键
  graph.bindKey('ctrl+z', () => {
    if (graph.canUndo()) {
      graph.undo();
    }
  });

  // 重做快捷键
  graph.bindKey('ctrl+y', () => {
    if (graph.canRedo()) {
      graph.redo();
    }
  });

  // 全选快捷键
  graph.bindKey('ctrl+a', () => {
    const cells = graph.getCells();
    graph.select(cells);
  });

  // 取消选择快捷键
  graph.bindKey('esc', () => {
    graph.cleanSelection();
  });

  // 放大快捷键
  graph.bindKey('ctrl+plus', () => {
    const zoom = graph.zoom();
    if (zoom < 3) {
      graph.zoom(0.1);
    }
  });

  // 缩小快捷键
  graph.bindKey('ctrl+minus', () => {
    const zoom = graph.zoom();
    if (zoom > 0.1) {
      graph.zoom(-0.1);
    }
  });

  // 适应窗口快捷键
  graph.bindKey('ctrl+0', () => {
    graph.zoomToFit({ padding: 20 });
  });

  // 实际大小快捷键
  graph.bindKey('ctrl+1', () => {
    graph.zoom(1);
  });
} 